import { ref, computed } from 'vue'

// 当前用户的权限列表
const userPermissions = ref([])

// 设置用户权限
export const setUserPermissions = (permissions) => {
  userPermissions.value = permissions
}

// 检查是否有权限
export const hasPermission = (permission) => {
  if (!permission) return true
  return userPermissions.value.includes(permission)
}

// 检查是否有任意一个权限
export const hasAnyPermission = (permissions) => {
  if (!permissions || permissions.length === 0) return true
  return permissions.some(permission => userPermissions.value.includes(permission))
}

// 检查是否拥有所有权限
export const hasAllPermissions = (permissions) => {
  if (!permissions || permissions.length === 0) return true
  return permissions.every(permission => userPermissions.value.includes(permission))
}

// 权限检查组合函数
export const usePermission = () => {
  return {
    hasPermission,
    hasAnyPermission,
    hasAllPermissions,
    userPermissions: computed(() => userPermissions.value)
  }
}

// 权限指令
export const permissionDirective = {
  mounted(el, binding) {
    const { value } = binding
    if (!value) return
    
    const permissions = Array.isArray(value) ? value : [value]
    const hasAuth = hasAnyPermission(permissions)
    
    if (!hasAuth) {
      el.parentNode && el.parentNode.removeChild(el)
    }
  }
}

// 权限映射表
export const permissionMap = {
  // 用户管理
  'user:management:add': 111,
  'user:management:export': 112,
  'user:management:import': 113,
  'user:management:batchDelete': 114,
  'user:management:search': 115,
  'user:management:reset': 116,
  'user:management:edit': 117,
  'user:management:view': 118,
  'user:management:delete': 119,
  
  // 用户档案
  'user:profile:view': 121,
  'user:profile:downloadPDF': 122,
  
  // 组织结构
  'user:structure:import': 131,
  'user:structure:save': 132,
  'user:structure:addSub': 133,
  'user:structure:edit': 134,
  'user:structure:delete': 135,
  
  // 心理测评 - 添加评测
  'evaluation:add:add': 211,
  'evaluation:add:selectClass': 212,
  'evaluation:add:reset': 213,
  'evaluation:add:confirm': 214,
  
  // 心理测评 - 报告记录
  'evaluation:report:search': 221,
  'evaluation:report:reset': 222,
  'evaluation:report:delete': 223,
  
  // 心理测评 - 团测进度
  'evaluation:progress:add': 231,
  'evaluation:progress:search': 232,
  'evaluation:progress:reset': 233,
  'evaluation:progress:completion': 234,
  'evaluation:progress:edit': 235,
  'evaluation:progress:delete': 236,
  'evaluation:progress:qrcode': 237,
  'evaluation:progress:sync': 238,
  
  // 心理测评 - 团体报告
  'evaluation:groupReport:search': 241,
  'evaluation:groupReport:refresh': 242,
  'evaluation:groupReport:view': 243,
  'evaluation:groupReport:downloadPDF': 244,
  
  // 预警干预 - 预警概览
  'intervention:overview:view': 311,
  
  // 预警干预 - 新增预警
  'intervention:addWarning:add': 321,
  'intervention:addWarning:export': 322,
  'intervention:addWarning:search': 323,
  'intervention:addWarning:reset': 324,
  
  // 预警干预 - 预警名单
  'intervention:warningList:view': 331,
  
  // 预警干预 - 预警评估
  'intervention:assessment:add': 341,
  'intervention:assessment:search': 342,
  'intervention:assessment:reset': 343,
  'intervention:assessment:import': 344,
  'intervention:assessment:downloadTemplate': 345,
  'intervention:assessment:save': 346,
  'intervention:assessment:edit': 347,
  'intervention:assessment:delete': 348,
  
  // 预警干预 - 预警干预
  'intervention:intervention:add': 351,
  'intervention:intervention:search': 352,
  'intervention:intervention:reset': 353,
  'intervention:intervention:upload': 354,
  'intervention:intervention:downloadTemplate': 355,
  'intervention:intervention:create': 356,
  'intervention:intervention:resetForm': 357,
  
  // 预警干预 - 预警设置
  'intervention:setting:refresh': 361,
  'intervention:setting:add': 362,
  'intervention:setting:save': 363,
  'intervention:setting:edit': 364,
  'intervention:setting:delete': 365,
  
  // 咨询预约 - 咨询师
  'consultation:counselor:add': 411,
  'consultation:counselor:search': 412,
  'consultation:counselor:reset': 413,
  'consultation:counselor:edit': 414,
  'consultation:counselor:delete': 415,
  
  // 咨询预约 - 预约记录
  'consultation:appointment:add': 421,
  'consultation:appointment:search': 422,
  'consultation:appointment:reset': 423,
  'consultation:appointment:confirm': 424,
  'consultation:appointment:cancel': 425,
  'consultation:appointment:selectUser': 426,
  'consultation:appointment:save': 427,
  
  // 咨询预约 - 咨询记录
  'consultation:record:add': 431,
  'consultation:record:search': 432,
  'consultation:record:reset': 433,
  'consultation:record:edit': 434,
  'consultation:record:delete': 435,
  'consultation:record:save': 436,
  
  // 心理课程
  'course:list:enter': 511,
  'course:list:back': 512,
  
  // 个人中心
  'person:center:edit': 611,
  'person:center:changePassword': 612,
  'person:center:saveInfo': 613,
  'person:center:confirmPassword': 614,
  
  // 权限管理
  'permission:role:search': 621,
  'permission:role:reset': 622,
  'permission:role:create': 623,
  'permission:role:edit': 624,
  'permission:role:toggle': 625,
  'permission:role:authorize': 626
}

// 根据权限ID获取权限键名
export const getPermissionKey = (permissionId) => {
  for (const [key, id] of Object.entries(permissionMap)) {
    if (id === permissionId) {
      return key
    }
  }
  return null
}

// 根据权限键名获取权限ID
export const getPermissionId = (permissionKey) => {
  return permissionMap[permissionKey] || null
}